package com.sephiron.clocksynchronizer;

import java.util.Calendar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.TextClock;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public class SetRealTimeNowConfirmFragment extends DialogFragment {
	public static final String TAG = "SetRealTimeNowConfirmFragment";
	public static final String EXTRA_YESNO = "com.sephiron.clocksynchronizer.yesno";
	public static final String EXTRA_PRESENT_TIME = "com.sephiron.clocksynchronizer.presenttime";

	private boolean yesno;
	TextClock mTextClock;
	MyDigitalClock mDigitalClock;
	DateFormat mDateFormat;

	public static SetRealTimeNowConfirmFragment newInstance() {
		SetRealTimeNowConfirmFragment fragment = new SetRealTimeNowConfirmFragment();
		return fragment;
	}

	private void sendResult(int resultCode) {
		if (getTargetFragment() == null) {
			return;
		}

		Intent i = new Intent();
		i.putExtra(EXTRA_YESNO, yesno);
		i.putExtra(EXTRA_PRESENT_TIME, Calendar.getInstance());
		getTargetFragment().onActivityResult(getTargetRequestCode(),
				resultCode, i);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		View v = getActivity().getLayoutInflater().inflate(
				R.layout.dialog_set_real_time_now, null);
		
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
			mDigitalClock = (MyDigitalClock) v.findViewById(R.id.dialog_set_real_time_now_digitalclock);
		} else {	
			mTextClock = (TextClock) v
					.findViewById(R.id.dialog_set_real_time_now_textclock);

			mTextClock.setFormat24Hour("HH:mm:ss");
			mTextClock.setFormat12Hour(null);
		}

		return new AlertDialog.Builder(getActivity())
				.setView(v)
				.setTitle(R.string.setRealTimeNow)
				.setMessage(R.string.setRealTimeNowQuestion)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								yesno = true;
								sendResult(Activity.RESULT_OK);
							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						})

				.create();
	}

}